import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.Arrays;

public class Demo10 {

    public static class map extends Mapper<LongWritable,Text,Text,Text>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] split = value.toString().split("\t");
            String v = split[1];
            String[] s = split[0].split("-");
            Arrays.sort(s);
            for (int i=0;i<s.length-1;i++){
                for (int j=i+1;j<s.length-i;j++){
                    String s1 = s[i] + "-" + s[j];
                    context.write(new Text(s1),new Text(v));
                }
            }
        }
    }

    public static class reduce extends Reducer<Text,Text,Text,Text>{
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            StringBuffer sb = new StringBuffer();
            for (Text value : values) {
                StringBuffer ad = sb.append(value.toString()).append("-");
            }
            context.write(key,new Text(sb.toString()));
        }
    }


    public static void main(String[] args) throws Exception{
        Job job = Job.getInstance();
        job.setJobName("共同好友2");
        job.setJarByClass(Demo10.class);

        job.setMapperClass(Demo10.map.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        job.setReducerClass(Demo10.reduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        FileInputFormat.addInputPath(job,new Path("/newfriend/part-r-00000"));
        FileOutputFormat.setOutputPath(job,new Path("/newfriend11"));

        job.waitForCompletion(true);
        System.out.println("OOKK2");

    }
}
